{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e1397c81",
   "metadata": {},
   "source": [
    "## Installation and deployment of 5G Core (5GC) of Open Air Interface (OAI)\n",
    "## Objectives\n",
    "- Install and configure the 5GC (version version 2.0.1) of OAI in a virtual machine Ubuntu 22.04 LTS \n",
    "- Deploy 5GC Core\n",
    "- Inspec 5G Core Docker containers by means of command line instructions \n",
    "- Install and configure Portainer tool\n",
    "- Inspec 5G Core Docker containers by means of Portainer browser"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c1e4be3",
   "metadata": {},
   "source": [
    "At the end of a set of three hands-on (this is the first one), the goal is to run the simulated OAI scenario, which consists of the 5G core (5GC), gNB in CU/DU split mode, and user equipment (UE), according to the image below: \n",
    "\n",
    "![5GC_01](../FIGS/H01_5GCore_UNI_III/5GC_deploy.jpg)\n",
    "\n",
    "Now, we are going to install, configure, deploy and interact to 5GC.\n",
    "\n",
    "\n",
    "## General Instructions\n",
    "\n",
    "In this task you will install and configure the core in its basic architecture according to the image below:\n",
    "\n",
    "![5GC_01](../FIGS/H01_5GCore_UNI_III/5GC_deploy_pods_01.png)\n",
    "\n",
    "Minimum hardware requirements (acording to OAI\n",
    "documentation):\n",
    "\n",
    "- Laptop/Desktop/Server for OAI CN5G and OAI gNB\n",
    "    - Operating System: Ubuntu 22.04 LTS\n",
    "    - CPU: 8 cores x86_64 @ 3.5 GHz\n",
    "    - RAM: 32 GB\n",
    "- Laptop for UE\n",
    "    - Operating System: Ubuntu 22.04 LTS\n",
    "    - CPU: 8 cores x86_64 @ 3.5 GHz\n",
    "    - RAM: 8 GB\n",
    "\n",
    "**However, if you do not have such hardware specifications, you may use the one below for both devices (tested for us, you may face some lags, but it works):**\n",
    "- Ubuntu 22.04.5 LTS;\n",
    "- CPU: 4 cores x86_64;\n",
    "- 8 GB RAM (or more)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1827c9c2",
   "metadata": {},
   "source": [
    "## CN5G Installation\n",
    "\n",
    "In a Linux Terminal, follow the steps below:\n",
    "\n",
    "**Step 0:** Setup your VM or Baremetal Machine, after install Useful Software\n",
    "\n",
    "**Step 1:** Install pre-requisites\n",
    "\n",
    "Open a terminal in your home directory and then execute the following commands:\n",
    "```\n",
    "sudo apt update -y\n",
    "```\n",
    "```\n",
    "sudo apt upgrade -y\n",
    "```\n",
    "```\n",
    "sudo apt install -y python3-pip git curl cmake tree golang vim net-tools build-essential nfs-common openssh-server putty git terminator\n",
    "```\n",
    "```\n",
    "sudo apt install -y ca-certificates curl gnupg\n",
    "```\n",
    "```\n",
    "sudo install -m 0755 -d /etc/apt/keyrings\n",
    "```\n",
    "```\n",
    "sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg\n",
    "```\n",
    "```\n",
    "sudo chmod a+r /etc/apt/keyrings/docker.gpg\n",
    "```\n",
    "```\n",
    "sudo echo \"deb [arch=\"$(dpkg --print-architecture)\" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \"$(. /etc/os-release && echo \"$VERSION_CODENAME\")\" stable\" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null\n",
    "```\n",
    "```\n",
    "sudo apt update\n",
    "```\n",
    "```\n",
    "sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin\n",
    "```\n",
    "```\n",
    "sudo curl -L https://github.com/docker/compose/releases/download/2.26.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose\n",
    "```\n",
    "```\n",
    "sudo usermod -a -G docker $(whoami)\n",
    "```\n",
    "\n",
    "**Step 2:** Reboot your operational system\n",
    "```\n",
    "reboot\n",
    "```\n",
    "**Step 3:** Download CN5G files\n",
    "\n",
    "Create a folder called OAI in your home directory:\n",
    "\n",
    "```\n",
    "mkdir ~/OAI\n",
    "```\n",
    "```\n",
    "cd OAI\n",
    "```\n",
    "```\n",
    "git clone https://gitlab.eurecom.fr/oai/cn5g/oai-cn5g-fed.git\n",
    "```\n",
    "```\n",
    "cd oai-cn5g-fed\n",
    "```\n",
    "```\n",
    "git checkout -f v2.1.0-1.2\n",
    "```\n",
    "**Step 4:** Pull the images from Docker Hub\n",
    "\n",
    "In the oai-cn5g-fed directory:\n",
    "```\n",
    "cd docker-compose\n",
    "```\n",
    "```\n",
    "sudo docker compose -f docker-compose-basic-nrf.yaml pull\n",
    "```\n",
    "You will see:\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_01.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1dd02a0f",
   "metadata": {},
   "source": [
    "## Deployment of 5GC\n",
    "\n",
    "**Step 1:** Start OAI CN5G\n",
    "In the CN5G terminal, type:\n",
    "\n",
    "```\n",
    "cd ~/OAI/oai-cn5g-fed/docker-compose\n",
    "```\n",
    "\n",
    "```\n",
    "sudo docker compose -f docker-compose-basic-nrf.yaml up -d\n",
    "```\n",
    "You will see:\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_02.png)\n",
    "\n",
    "\n",
    "**Optional step: (if you need)** Stop OAI 5GC\n",
    "\n",
    "In the CN5G terminal, type:\n",
    "```\n",
    "sudo docker compose -f docker-compose-basic-nrf.yaml down\n",
    "```\n",
    "You will see:\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_05.png)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea2ab4e5",
   "metadata": {},
   "source": [
    "## Verificação dos Pods com linha de comando e com o Portainer\n",
    "\n",
    "The 5GC components were installed as docker containers. A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings. Container images become containers at runtime and in the case of Docker containers – images become containers when they run on Docker Engine. Available for both Linux and Windows-based applications, containerized software will always run the same, regardless of the infrastructure. Containers isolate software from its environment and ensure that it works uniformly despite differences for instance between development and staging.\n",
    "\n",
    "Containers and virtual machines have similar resource isolation and allocation benefits, but function differently because containers virtualize the operating system instead of hardware. Containers are more portable and efficient. Figure bellow illustrates the difference between VMs and containers.\n",
    "\n",
    "![5GC_deploy_04](../FIGS/H01_5GCore_UNI_III/5GC_deploy_pods_04.png)\n",
    "\n",
    "**Step 1**: to see all 5GC docker containers by command line you could type the following command in a terminal:\n",
    "\n",
    "```\n",
    "sudo docker ps -a\n",
    "```\n",
    "\n",
    "The results are as bellow:\n",
    "\n",
    "![5GC_deploy_03](../FIGS/H01_5GCore_UNI_III/5GC_deploy_pods_03.png)\n",
    "\n",
    "You can see all virtual functions that comprise the 5GC.\n",
    "\n",
    "\n",
    "**Step 2**: let's logs of a running docker container, for instance, let's see logs of the 5GC's AMF container. In a terminal , type:\n",
    "\n",
    "```\n",
    "sudo docker logs -f 2de7bfa9afa4\n",
    "```\n",
    "\n",
    "You will see a screen similar to the figure bellow.\n",
    "\n",
    "![5GC_deploy_05](../FIGS/H01_5GCore_UNI_III/5GC_deploy_pods_05.png)\n",
    "\n",
    "\n",
    "You can find the same results with the command:\n",
    "\n",
    "```\n",
    "sudo docker logs -f oai-amf\n",
    "```\n",
    "\n",
    "**Step 3**: you can also see the hardware resources all containers are consuming. In a terminal , type:\n",
    "\n",
    "```\n",
    "sudo docker container stats\n",
    "```\n",
    "\n",
    "You will see a screen similar to the figure bellow.\n",
    "\n",
    "![5GC_deploy_06](../FIGS/H01_5GCore_UNI_III/5GC_deploy_pods_06.png)\n",
    "\n",
    "To return to terminal, type: crtl+c\n",
    "\n",
    "**Step 4**: let's execute a command inside a running docker container, for instance, let's open a bash inside the 5GC's AMF container and see the processes in execution. See in figures above that **2de7bfa9afa4** is the container ID of the 5GC's AMF container. So, in a terminal , type (ps.: realize that you need to see the containerID of 5GC's AMF installed in your machine):\n",
    "\n",
    "```\n",
    "sudo docker exec -it 2de7bfa9afa4 bash\n",
    "```\n",
    "\n",
    "You will see a screen similiar to the figure bellow.\n",
    "\n",
    "![5GC_deploy_07](../FIGS/H01_5GCore_UNI_III/5GC_deploy_pods_07.png)\n",
    "\n",
    "As you have a bash shell inside the container, you can type bash commands. Let's see all processes running by this container. Type:\n",
    "\n",
    "```\n",
    "top\n",
    "```\n",
    "\n",
    "You will see a screen similiar to the figure bellow.\n",
    "\n",
    "![5GC_deploy_08](../FIGS/H01_5GCore_UNI_III/5GC_deploy_pods_08.png)\n",
    "\n",
    "To return to terminal, type: crtl+c\n",
    "\n",
    "Type exit to leave the container bash shell and return to the terminal.\n",
    "\n",
    "\n",
    "**Step 5:** A good Docker Cheat Sheet can be found [here](https://docs.docker.com/get-started/docker_cheatsheet.pdf).\n",
    "\n",
    "\n",
    "**Step 6 (optional)**: there is a plenty of graphical tool to manager, see and operate dockers. Let's install and use Portainer. To install it, first, create the volume that Portainer Server will use to store its database. Ia a terminal, type:\n",
    "\n",
    "```\n",
    "sudo docker volume create portainer_data\n",
    "```\n",
    "\n",
    "**Step 7 (optional)**: Then, download and install the Portainer Server container:\n",
    "\n",
    "```\n",
    "sudo docker volume create portainer_data\n",
    "```\n",
    "\n",
    "```\n",
    "sudo docker run -d -p 8000:8000 -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest\n",
    "```\n",
    "\n",
    "More details [in this link.](https://docs.portainer.io/start/install-ce/server/docker/linux)\n",
    "\n",
    "**Step 8 (optional)**: Now that the installation is complete, you can log into your Portainer Server instance by opening a web browser and going to:\n",
    "\n",
    "https://localhost:9443\n",
    "\n",
    "\n",
    "**Step 9 (optional)**: You need to create a user. For this, you will see a screen similar to:\n",
    "\n",
    "![5GC_port_8](../FIGS/H01_5GCore_UNI_III/5GC_deploy_port_08.png)\n",
    "\n",
    "**Step 10 (optional)**: Now, you can see the initial screen and you could proceedd using the local environment. Please, click on **Get Started**.\n",
    "\n",
    "![5GC_port_9](../FIGS/H01_5GCore_UNI_III/5GC_deploy_port_09.png)\n",
    "\n",
    "**Step 11 (optional)**: then, you can see the screen with the local environment. Click on it.\n",
    "\n",
    "![5GC_port_10](../FIGS/H01_5GCore_UNI_III/5GC_deploy_port_10.png)\n",
    "\n",
    "**Step 12 (optional)**: Now, you could see the environment dashboard with all information about your Portainer. Let's see all Containers clicking on Containers menu as indicating below.\n",
    "\n",
    "![5GC_port_11](../FIGS/H01_5GCore_UNI_III/5GC_deploy_port_11.png)\n",
    "\n",
    "**Step 13 (optional)**: you can see all containers, their status and a menu of quick actions. \n",
    "\n",
    "![5GC_port_12](../FIGS/H01_5GCore_UNI_III/5GC_deploy_port_12.png)\n",
    "\n",
    "**Step 14 (optional)**: To see the AMF container logs, please click on icone indicated in the figure below.\n",
    "\n",
    "![5GC_port_13](../FIGS/H01_5GCore_UNI_III/5GC_deploy_port_13.png)\n",
    "\n",
    "**Step 15 (optional)**: Finally, you can see AMF logs (note that you can filter and download it).\n",
    "\n",
    "![5GC_port_14](../FIGS/H01_5GCore_UNI_III/5GC_deploy_port_14.png)\n",
    "\n",
    "An interesting video about Portainer tool is: [Controle seus containers de maneira SIMPLES com o Portainer - Diolinux Labs](https://www.youtube.com/watch?v=q0LrLN4fylw),\n",
    "\n",
    "# Deliverable 01:\n",
    "See the log of all docker containers of 5GC and identify the following information:\n",
    "\n",
    "- GUAMI (MCC, MNC, Region ID, AMF Set ID, AMF pointer):\n",
    "- Slice Support: what are SST, SD supported? Explain those parameters. Indicate the 5QI of each slice.\n",
    "- What is the N2 IP Address and port?\n",
    "- What is the NRF IP Address and port?\n",
    "- What is the AUSF IP Address and port?\n",
    "- What is the AMF IP Address and port?\n",
    "- What is the N4 IP Address and port?\n",
    "\n",
    "Show the logs that you see all information."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
